﻿using System;
using System.Collections.Generic;
using System.Linq;
using KLS.PPDS.ApplicationService.Manager;
using KLS.PPDS.Core.Entity;


namespace KLS.PPDS.ApplicationService.Provider
{
    public class CustomRoleProvider : System.Web.Security.RoleProvider
    {
        private string _applicationName;

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }



        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            var arr = new string[] { "superadmin", "two", "three" };
            return arr;
        }

        public override string[] GetAllRoles()
        {
            var arr = new string[] { "superadmin", "two", "three" };
            return arr;
        }

        public override string[] GetRolesForUser(string username)
        {
            List<string> roles = new List<string>();
            User u = UserManager.Get(username);
            if (u != null)
            {
                roles.Add(u.Role.RoleName);
            //    foreach (UserGroupRel g in u.Groups)
            //    {
            //        if (g.Group!=null)
            //        {
            //            string[] list = GetRolesForGroup(g.Group.GroupId);
            //            foreach (string r in list)
            //            {
            //                if (!roles.Contains(r))
            //                {
            //                    roles.Add(r);
            //                }
            //            } 
            //        }
            //    }
            }
            return roles.ToArray();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            IList<string> userNames = new List<string>();
            //string[] groupNames = GetGroupsInRole(roleName);
            //foreach (string name in groupNames)
            //{
            //    KLS.PPDSGroup g = ProtalGroupManager.Get(name);
            //    foreach (KLS.PPDSUser u in g.Users)
            //    {
            //        if (!userNames.Contains(u.UserName))
            //        {
            //            userNames.Add(u.UserName);
            //        }
            //    }
            //}
            return userNames.ToArray();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            //if (KLS.PPDSContext.CurrentUser.UserType == UserType.SuperAdmin)
            //{
            //    return true;
            //}
            //else
            //{
            //    User user = UserManager.Get(string.IsNullOrEmpty(username) ? KLS.PPDSContext.CurrentUser.Name : username);

            //    if (user == null)
            //    {
            //        return false;
            //    }
            //    return user.Groups.Any(group => IsGroupInRole(group.GroupId, ExcludeViewForModule(roleName)));
            //}


            return true;
        }


        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            return true;
        }







        public override string ApplicationName
        {
            get { return _applicationName; }
            set { _applicationName = value; }
        }

        public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)
        {
            _applicationName = config["applicationName"];
            if (string.IsNullOrEmpty(_applicationName))
            {
                _applicationName = ProviderUtility.GetDefaultAppName();
            }

            base.Initialize(name, config);
        }


    }
}
